﻿      function showAjax(idclass,link)
      {
                $(idclass).html("<img src='/images/loading1.gif' />");
                $(idclass).load(link);
      }
      function addClass(idchu,idthem)
      {
        $("p").addClass("myClass yourClass");
      }
      function hidenClass(idclasshidden)
      {
        $(idclasshidden).hide();
        //bien mat
      }
      function showClass(idclassshow)
      {
        $(idclassshow).show();
        //hienthi
      }
      function hiden2Class(idclasshidden1,idclasshidden2)
      {
        $(idclasshidden1).hide();
        $(idclasshidden2).hide();
        //bien mat
      }
      function show2Class(idclassshow1,idclassshow2)
      {
        $(idclassshow1).show();
        $(idclassshow2).show();
        //hienthi
      }
       function showClassSubMenu(idclassshow,id)
      {
        $(idclassshow).show();
        showAjax('#loadsubmenu',"../services/menu/submenu.aspx?idmenu="+id);
        //hienthi
      }
 function showPopUp(path)
{   
    $.facebox.settings.opacity = 0.2
    $.facebox.settings.overlay = true;	
    jQuery.facebox({ ajax: path},'popupGhiAmFace');        
    return false;
}
function findSP(id)
{
    window.location='../home/timkiem.aspx?tukhoa='+$(id).val();
}
function doSearchEnter(e,keyword)
{  
      var keyCode = (window.Event) ? e.which : e.keyCode;      
      if( keyCode == 13 ) 
      {
        if(keyword==''){alert('Bạn chưa nhập điều kiện tìm kiếm');return;};
        window.location='../home/timkiem.aspx?tukhoa='+keyword;
      }
  }
  function addLikeShare(id, idShow) {
      $.get("/Service/share/AddLike.aspx?id=" + id, function(result) {
          if (result == 0) alert('Bạn like thất bại!');
          else {
              //alertInfo('Bạn đã like thành công!');
              //div.text("Đã Xóa");
              var numberLike = $('#setLike' + id).text();
              var numberLikeNew = parseInt(numberLike) + 1;
              $('#setLike' + id).text(numberLikeNew);
              $(idShow).empty();
          }
      });
  }
  //cookie
  function setCookie(c_name, value, exdays) {
      var exdate = new Date();
      exdate.setMinutes(exdate.getMinutes() + exdays);
      var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
      document.cookie = c_name + "=" + c_value;
  }

  function getCookie(c_name) {
      var c_value = document.cookie;
      var c_start = c_value.indexOf(" " + c_name + "=");
      if (c_start == -1) {
          c_start = c_value.indexOf(c_name + "=");
      }
      if (c_start == -1) {
          c_value = null;
      }
      else {
          c_start = c_value.indexOf("=", c_start) + 1;
          var c_end = c_value.indexOf(";", c_start);
          if (c_end == -1) {
              c_end = c_value.length;
          }
          c_value = unescape(c_value.substring(c_start, c_end));
      }
      return c_value;
  }

function UpdateVoteByID_Hung(id, sIdHtml) {
    FB.getLoginStatus(function (response) {
        var page_id = "150159721818778";
        if (response && response.authResponse) {
            var user_id = response.authResponse.userID;
            var fql_query = "SELECT uid FROM page_fan WHERE page_id = " + page_id + "and uid=" + user_id;
            FB.Data.query(fql_query).wait(function (rows) {
                if (rows.length == 1 && rows[0].uid == user_id) {
                    //console.log('Liked');
                    letUpdate(id, sIdHtml,'Bạn đã Vote thành công !!');
                }
                else {
                    //console.log('Not Like');
                    $('#basic-modal-content').modal();
                    FB.Event.subscribe('edge.create', function (response, widget) {
                        if (response == 'https://www.facebook.com/conyeu.yeucon') {
                            
                            $('.messFB').show();
                            letUpdate(id, sIdHtml,'');
                        }
                    });
                }
            });
        } else {
            FB.login(function (response) {
                if (response && response.authResponse) {
                    var user_id = response.authResponse.userID;
                    var fql_query = "SELECT uid FROM page_fan WHERE page_id = " + page_id + "and uid=" + user_id;
                    FB.Data.query(fql_query).wait(function (rows) {
                        if (rows.length == 1 && rows[0].uid == user_id) {
                            //console.log('Liked');
                            letUpdate(id, sIdHtml, 'Bạn đã Vote thành công !!');
                        }
                        else {
                            //console.log('Not Like');
                            $('#basic-modal-content').modal();
                            FB.Event.subscribe('edge.create', function (response, widget) {
                                if (response == 'https://www.facebook.com/conyeu.yeucon') {
                                    $('.messFB').show();
                                    letUpdate(id, sIdHtml,'');
                                }
                            });
                        }
                    });
                }
            }, { scope: 'publish_actions, user_likes', display: 'popup' });
        }
    });
    
}

function letUpdate(id, sIdHtml,message) {
    var checkCookieCM = getCookie("votedetail" + id);
    if (checkCookieCM != null && checkCookieCM != "") {
        alert('Bạn đã vote ảnh này rồi hãy đợi 15 phút để vote tiếp!');
    }
    else {
        //chua binh luan
        $.get("/Services/UpdateVote_Hung.aspx?id=" + id, function (result) {
            if (result == 0) {
                alert('Bạn vote thất bại!');
            }
            else if (result > 0) {
                if (message != null && message != '')
                    alert(message);
                $('.' + sIdHtml).text(result);
                setCookie("votedetail" + id, id, 15);
            }
            else {
                alert('Hiện tai chưa thể vote!');
            }
        });
    }
}
